/**
 * 资源管理新增页面JS
 * User: fengchuang
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_RESOURCE_INFO");
    initSelect2();

    $("#saveBtn").click(function(){
        save();
    });
    $("#goBackBtn, #headGoBackBtn").click(function(){
        window.forward(springUrl+ "/web/resourceInfo/index");
    });
    initData();
});

function initData(){
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        url: springUrl+"/web/resourceInfo/detail",
        data: {
            uniqueNo: $('#uniqueNo').val()
        },
        success: function (data) {
            $("#appTypeCode").select2("val", data.appTypeCode);
            $("#parentResourceCode").val(data.resourceCode);
        },
        error: function(data){
            Alert.error("初始化数据失败!");
        }
    });
}

/**
 * 保存资源
 */
function save(){
    if($('#addForm').trigger("validate").isValid()){
        var index =  layer.msg('正在处理中...', {icon: 16, shade: 0.3, time: 0});
        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: springUrl + "/web/resourceInfo/insert",
            data: {
                resourceCode: $('#resourceCode').val(),
                resourceName: $('#resourceName').val(),
                resourceTypeCode: $('#resourceTypeCode').val(),
                resourceTypeName: $('#resourceTypeCode').val()!="" ? $('#resourceTypeCode option:selected').text() : "",
                parentResourceCode: $('#parentResourceCode').val(),
                appTypeCode: $('#appTypeCode').val(),
                appTypeName: $('#appTypeCode').val()!="" ? $('#appTypeCode option:selected').text() : "",
                resourceUrl: $('#resourceUrl').val(),
                resourceSort: $('#resourceSort').val(),
                resourceIcon: $('#resourceIcon').val(),
                bizCategoryCode: $('#bizCategoryCode').val(),
                bizCategoryName: $('#bizCategoryCode').val()!="" ? $('#bizCategoryCode option:selected').text() : ""
            },
            success: function (data) {
                layer.close(index);
                Alert.success("数据保存成功!", function(){
                    window.forward(springUrl+ "/web/resourceInfo/index");
                });
            },
            error: function(data){
                layer.close(index)
                Alert.error(data.responseText);
            }
        });
    }
}